import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Axios from "axios";
import Api from "./api";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import echarts from 'echarts'

Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.prototype.$echarts=echarts



Axios.defaults.baseURL = "http://172.16.6.122:8000";
Axios.defaults.timeout = 5000;

/* router.beforeEach(function (to, from, next) {
  if (to.name === 'Login') {
    next();
  } else {
    if (sessionStorage.getItem('isLogin')) {
      next();
    } else {
      next({
        path: '/login'
      })
    }
  }
}); */

const vm = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

Axios.interceptors.request.use(
  request => {
    vm.$store.dispatch("loadingStart"); // 显示loading组件
    /* if (store.getters.token) {
      request.headers['X-Token'] = getToken() // 让每个请求携带token--['X-Token']为自定义key
    } */
    //request.headers["token"] = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzZWNvbmQiLCJVc2VyIjp7ImN1cnJlbnRQYWdlIjowLCJwYWdlU2l6ZSI6MCwiaWQiOjAsInJvbGVJZHMiOm51bGwsInVzZXJOYW1lIjpudWxsLCJzaG9wTmFtZSI6bnVsbCwidXNlck51bSI6bnVsbCwidXNlcklkQ2FyZCI6bnVsbCwidXNlclBob25lIjpudWxsLCJ1c2VyU3RhdHVzIjoxLCJuYW1lIjpudWxsLCJwYXNzd29yZCI6IjQyOTdGNDRCMTM5NTUyMzUyNDVCMjQ5NzM5OUQ3QTkzIiwidXNlckdyb3VwIjpudWxsLCJsb2dpbkFjY291bnQiOiJsaXNpIiwicm9sZU5hbWUiOm51bGx9LCJleHAiOjE2MDEyNzczMjMsImlhdCI6MTYwMTI3NzMyMSwianRpIjoiand0In0.yTKNPmtEB9PJQA32B47c_VOseg079dN31lzGVpcTYGI";
    request.headers["Content-Type"] = "application/json";
    return request;
  },
  error => {
    vm.$store.dispatch("loadingEnd");
    return Promise.reject(error);
  }
);

//4.2 添加响应拦截器
Axios.interceptors.response.use(
  response => {
    vm.$store.dispatch("loadingEnd");
    return response;
  },
  error => {
    vm.$store.dispatch("loadingEnd");
    return Promise.reject(error);
  }
);

Vue.prototype.$api = Api;
Vue.prototype.$axios = Axios;
